//
//  MessageAudioCSUView.m
//  SouthChina
//
//  Created by creator_lq on 14/12/17.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "MessageAudioCSUView.h"

@implementation MessageAudioCSUView
{
    
    NSDate* recoridTime;
}

@synthesize voiceImage;
@synthesize micImage;
@synthesize cancleImage;
@synthesize hitText;
@synthesize recoder;
@synthesize sayButtom;
@synthesize delegate;



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        backgroundView.userInteractionEnabled = YES;
        backgroundView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        backgroundView.image = [UIImage imageNamed:@"message_say_back.png"];
        [self addSubview:backgroundView];
        
        CGRect sayToolFrame = self.frame;
        micImage = [[UIImageView alloc] initWithFrame:CGRectMake(5,sayToolFrame.size.height/13,sayToolFrame.size.height/3,sayToolFrame.size.width/3)];
        micImage.image = [UIImage imageNamed:@"mic.png"];
        [micImage sizeToFit];
        [self addSubview:micImage];
        
        voiceImage = [[UIImageView alloc] initWithFrame:CGRectMake(sayToolFrame.size.height/2,sayToolFrame.size.height/13,sayToolFrame.size.height/3,sayToolFrame.size.width/3)];
        voiceImage.image = [UIImage imageNamed:@"amp7.png"];
        voiceImage.contentMode = UIViewContentModeScaleAspectFit;
        [voiceImage sizeToFit];
        [self addSubview:voiceImage];
        
        cancleImage = [[UIImageView alloc] initWithFrame:CGRectMake(sayToolFrame.size.height/4,sayToolFrame.size.height/6,sayToolFrame.size.width/2,sayToolFrame.size.width/2-10.0)];
        cancleImage.image = [UIImage imageNamed:@"message_cancle.png"];
        [self addSubview:cancleImage];
        
        hitText = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, sayToolFrame.size.height*3/4,sayToolFrame.size.width-20.0, sayToolFrame.size.height/4-10.0) ];
        [hitText setTitle:@"手指上滑，取消发送" forState:UIControlStateNormal];
        [hitText setBackgroundImage:nil forState:UIControlStateNormal];
        hitText.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [self addSubview:hitText];
        [self setnomalSendImage];
    }
    return self;
}

-(void)setSayButtomAction:(UIButton *)saybuttom{
    
    self.sayButtom = saybuttom;
    [self.sayButtom addTarget:self action:@selector(sayTouchDown) forControlEvents:UIControlEventTouchDown];
    [self.sayButtom addTarget:self action:@selector(sayTouchDragInside) forControlEvents:UIControlEventTouchDragInside];
    [self.sayButtom addTarget:self action:@selector(sayTouchDragExit) forControlEvents:UIControlEventTouchDragExit];
    [self.sayButtom addTarget:self action:@selector(sayTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self.sayButtom addTarget:self action:@selector(sayTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
}

//在按钮内抬起 --发送录音
-(void)sayTouchUpInside{
    self.hidden = YES;
    NSURL *url = [recoder completeRecoder];
    
    NSDate *curData = [NSDate date];
    NSTimeInterval secondsBetweenDates= [curData timeIntervalSinceDate:recoridTime];
    if(delegate){
        
        [delegate onRecordComplete:url recoder:secondsBetweenDates];
    }
}

#pragma mark - mp3 record
-(void)setcancelSendImage{
    voiceImage.hidden = YES;
    micImage.hidden = YES;
    cancleImage.hidden = NO;
    [hitText setTitle:@"松开手指，取消发送" forState:UIControlStateNormal];
    [hitText setBackgroundImage:[UIImage imageNamed:@"say_warming.png"] forState:UIControlStateNormal];
}
-(void)setnomalSendImage{
    voiceImage.hidden = NO;
    micImage.hidden = NO;
    cancleImage.hidden = YES;
    [hitText setTitle:@"手指上滑，取消发送" forState:UIControlStateNormal];
    [hitText setBackgroundImage:nil forState:UIControlStateNormal];
}
//更新音量（图片显示）
-(void)detectionVoiceWithRecoder:(NSInteger) volue{

     
}

//按下--开始录影
-(void)sayTouchDown{
    
    recoridTime = [NSDate date];
    NSLog(@"按下");
    self.hidden = NO;
    [self setnomalSendImage];
    recoder = [[MessageAudioRecoder alloc] init];
    recoder.delegate  = self;
    BOOL statue = [recoder startRecoder];
    if (statue) {
        NSLog(@"成功");
    }else{
        NSLog(@"失败");
    }
}
//在按;钮内按下 --继续录音
-(void)sayTouchDragInside{
    [self setnomalSendImage];
}
//移动到按钮外 --暂停录音
-(void)sayTouchDragExit{
    NSLog(@"移动到按钮外");
    [self setcancelSendImage];
}

//在按钮外抬起 --取消发送
-(void)sayTouchUpOutside{
    NSLog(@"在按钮外抬起");
    self.hidden = YES;
    [recoder completeRecoder];
}


@end
